/*
*    Qizx/open 3.0 beta
*
* This code is the open-source version of Qizx.
* Copyright (C) 2004-2009 Axyana Software -- All rights reserved.
*
* The contents of this file are subject to the Mozilla Public License 
*  Version 1.1 (the "License"); you may not use this file except in 
*  compliance with the License. You may obtain a copy of the License at
*  http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
*  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
*  for the specific language governing rights and limitations under the
*  License.
*
* The Initial Developer of the Original Code is Xavier Franc - Axyana Software.
*
*/
using System;
using QName = System.Qizx.Api.QName;
using XMLPullStream = System.Qizx.Api.XMLPullStream;
namespace System.Qizx.Xdm
{
	
	public abstract class XMLPullStreamBase : XMLPullStream
	{
		virtual public int CurrentEvent
		{
			get
			{
				return curEvent;
			}
			
		}
		virtual public string Encoding
		{
			get
			{
				return encoding;
			}
			
		}
		virtual public string DTDName
		{
			get
			{
				return dtdName;
			}
			
		}
		virtual public string DTDPublicId
		{
			get
			{
				return dtdPublicId;
			}
			
		}
		virtual public string DTDSystemId
		{
			get
			{
				return dtdSystemId;
			}
			
		}
		virtual public string InternalSubset
		{
			get
			{
				return dtdInternalSubset;
			}
			
		}
		virtual public int AttributeCount
		{
			get
			{
				if (attrCount < 0)
					lazyAttr();
				return attrCount;
			}
			
		}
		virtual public int NamespaceCount
		{
			get
			{
				if (nsCount < 0)
					lazyNS();
				return nsCount;
			}
			
		}
		virtual public string Text
		{
			get
			{
				if (charBuffer == null)
					return null;
				return new System.String(charBuffer, 0, charCount);
			}
			
		}
		virtual public int TextLength
		{
			get
			{
				return charCount;
			}
			
		}
		virtual public string Target
		{
			get
			{
				return piTarget;
			}
			
		}
		public abstract System.Qizx.Api.QName Name{get;}
		protected internal int curEvent;
		protected internal string encoding;
		protected internal string dtdName;
		protected internal string dtdPublicId;
		protected internal string dtdSystemId;
		protected internal string dtdInternalSubset;
		
		protected internal char[] charBuffer;
		protected internal int charCount;
		protected internal string piTarget;
		
		protected internal int attrCount;
		private QName[] attrNames = new QName[2];
		private System.String[] attrValues = new System.String[2];
		
		protected internal int nsCount;
		private QName[] namespaces; // uses prefix and uri of QNames
		
		protected internal virtual int setEvent(int event_Renamed)
		{
			return curEvent = event_Renamed;
		}
		
		public virtual QName getAttributeName(int index)
		{
			if (attrCount < 0)
				lazyAttr();
			return attrNames[index];
		}
		
		public virtual string getAttributeValue(int index)
		{
			if (attrCount < 0)
				lazyAttr();
			return attrValues[index];
		}
		
		/// <summary> To be overriden if appropriate</summary>
		protected internal virtual void  lazyAttr()
		{
		}
		
		protected internal virtual void  addAttribute(QName name, string value_Renamed)
		{
			if (attrCount >= attrNames.Length)
			{
				QName[] old = attrNames;
				attrNames = new QName[old.Length * 2];
				Array.Copy(old, 0, attrNames, 0, old.Length);
				System.String[] oldValues = attrValues;
				attrValues = new System.String[attrNames.Length];
				Array.Copy(oldValues, 0, attrValues, 0, oldValues.Length);
			}
			attrNames[attrCount] = name;
			attrValues[attrCount++] = value_Renamed;
		}
		
		public virtual string getNamespacePrefix(int index)
		{
			if (nsCount < 0)
				lazyNS();
			return namespaces[index].Name;
		}
		
		public virtual string getNamespaceURI(int index)
		{
			if (nsCount < 0)
				lazyNS();
			return namespaces[index].Namespace;
		}
		
		protected internal virtual void  lazyNS()
		{
		}
		
		protected internal virtual void  addNamespace(string prefix, string namespaceURI)
		{
			QName ns = IQName.GetQName(namespaceURI, prefix);
			if (nsCount >= namespaces.Length)
			{
				QName[] old = namespaces;
				namespaces = new QName[old.Length * 2];
				Array.Copy(old, 0, namespaces, 0, old.Length);
			}
			namespaces[nsCount++] = ns;
		}
		public abstract int moveToNextEvent();
	}
}